/*
 * Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Eclipse Public License
 * Version 1.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://opensource.org/licenses/EPL-1.0 and read it before using this file.
 */

#include "stop_watch.h"
#include <Windows.h>


StopWatch::StopWatch()
    : m_elapsed( 0 ), m_start( 0 ), m_spanStart( 0 ), m_isRunning( false )
{
}

void StopWatch::start()
{
    m_elapsed = 0;
    m_spanStart = m_start = ::GetTickCount();
    m_isRunning = true;
}

void StopWatch::stop()
{
    if ( m_isRunning )
    {
        m_isRunning = false;
        m_elapsed = ::GetTickCount() - m_start;
    }
}

void StopWatch::reset()
{
    stop();
    m_start = 0;
    m_elapsed = 0;
}

void StopWatch::restart()
{
    stop();
    start();
}

uint_t StopWatch::getElapsed()
{
    return m_isRunning ? ::GetTickCount() - m_start : m_elapsed;
}

uint_t StopWatch::getShortElapsed()
{
    uint_t result = 0;

    if ( m_isRunning )
    {
        uint_t newSpanStart = ::GetTickCount();

        result = newSpanStart - m_spanStart;

        m_spanStart = newSpanStart;
    }
    return result;
}
